﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL.Banco;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.Admin.Proyecto
{
    public partial class CuentaFinanciamientoGrid : System.Web.UI.UserControl
    {
        public CuentaFinanciamientoGrid()
        {
            bol = new CuentaFinanciamientoBL();
        }

        private CuentaFinanciamientoBL bol;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {

                idProyecto.Value = (string)Request.QueryString["id"];
                FormatForm();
            }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
            
        }
       
        
        private void FormatForm()
        {
                BindDataItem();
                setFooterRow();
        }


        protected void AddItem(object sender, EventArgs e)
        {
            try
            {

               
                 RadioButtonList idDocumentoTipo =
                    ((RadioButtonList)itemGridView.FooterRow.FindControl("tipoCIRBTN"));
                 TextBox cuotaInicalTXT =
                    ((TextBox)itemGridView.FooterRow.FindControl("cuotaInicalTXT"));
                 DropDownList BancoDDL =
                    ((DropDownList)itemGridView.FooterRow.FindControl("BancoDDL"));
                 TextBox comentarioTXT =
                    ((TextBox)itemGridView.FooterRow.FindControl("comentarioTXT"));

                 CheckBox enCuentaRBTN =
                     ((CheckBox)itemGridView.FooterRow.FindControl("enCuentaRBTN"));

                bool bitEsMontoInicial = idDocumentoTipo.SelectedValue == "0" ? true : false;
                bool bitCuotaEnCuenta = enCuentaRBTN.Checked;
                
                bol.Add(idProyecto.Value.ToInt(),
                        bitEsMontoInicial,
                        cuotaInicalTXT.Text.ToDecimal().Value,
                        BancoDDL.SelectedValue.ToInt(),
                        comentarioTXT.Text,
                        bitCuotaEnCuenta);

                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());


                RadioButtonList tipoCIRBTN =
                     ((RadioButtonList)gv.Rows[e.RowIndex].FindControl("tipoCIRBTN"));
                TextBox cuotaInicalTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("cuotaInicalTXT"));
                TextBox comentarioTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("comentarioTXT"));

              

                CheckBox enCuentaRBTN =
                   ((CheckBox)itemGridView.Rows[e.RowIndex].FindControl("enCuentaRBTN"));
            

                  bool bitEsMontoInicial = tipoCIRBTN.SelectedValue == "0" ? true : false;
                  bool bitCuotaEnCuenta = enCuentaRBTN.Checked;


                bol.Update(idinternal, bitEsMontoInicial,cuotaInicalTXT.Text.ToDecimal().Value,comentarioTXT.Text, bitCuotaEnCuenta);
                
                gv.EditIndex = -1;

                BindDataItem();
                setFooterRow();
                 GridUpdatePanel.Update();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;

                BindDataItem();
                itemGridView.FooterRow.Visible = false;
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                BindDataItem();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                bol.Delete(idInternal);

                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<CuentaFinanciamientoInfo> itemList0 = new List<CuentaFinanciamientoInfo>();
                CuentaFinanciamientoInfo itemcero = new CuentaFinanciamientoInfo();
                itemcero.idInternal = 0;
                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }

        public void BindDataItem()
        {


            itemGridView.DataSource = bol.GetCuentaFinanciacionList(idProyecto.Value.ToInt());
            itemGridView.DataBind();
            resetEmpty();

        }
        private void setFooterRow()
        {

            var bancos = new ListValueBL().GetBanco();


            DropDownList BancoDDL = ((DropDownList)itemGridView.FooterRow.FindControl("BancoDDL"));


            BancoDDL.DataValueField = "idInternal";
            BancoDDL.DataTextField = "descripcion";
            BancoDDL.DataSource = bancos;
            BancoDDL.DataBind();


        }

    }

}